package com.liangzi.servicebase.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    // 获取 redisTemplate
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    // 设置缓存
    public void set(String key,Object value){
        redisTemplate.opsForValue().set(key,value);
    }
    // 设置缓存 及其有效时间
    public void set(String key,Object value,long timeout){
        redisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
    }
    // 获取缓存
    public Object get(String key){
        if(!redisTemplate.hasKey(key)){return null;}
        return  redisTemplate.opsForValue().get(key);
    }

}
